package com.miqtech.master.wyrecorder;

import android.app.Application;
import android.content.Context;

import com.bugtags.library.Bugtags;
import com.miqtech.master.wyrecorder.constants.API;
import com.miqtech.master.wyrecorder.constants.Constants;
import com.miqtech.master.wyrecorder.nim.NIMHelper;
import com.miqtech.master.wyrecorder.utils.Utils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.squareup.leakcanary.LeakCanary;

/**
 * Created by arvin on 2016/10/20.
 */

public class WYRecorderApp extends Application {
    private static Context mContext;

    public static WYRecorderApp getContext() {
        return (WYRecorderApp) mContext;
    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(4 * 1024 * 1024).threadPoolSize(3)
                .diskCacheExtraOptions(480, 320, null)
                .build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initApp();
    }

    private void initApp() {
        mContext = this;
        if (Utils.inMainProcess()) {
            LeakCanary.install(this);
            initBugTags();
            initImageLoader(this);
        }
        NIMHelper.getInstance().init();
    }

    private void initBugTags() {
        if (BuildConfig.DEBUG || !API.HOST.startsWith("http://yuerapi")) {
            Bugtags.start(Constants.KEY_BUGTAGS_BETA, this, Bugtags.BTGInvocationEventNone);
        } else {
            Bugtags.start(Constants.KEY_BUGTAGS_LIVE, this, Bugtags.BTGInvocationEventNone);
        }
    }

}
